package com.zzz.offer.lengthOfLongestSubstring;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abba"));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s == null || s.equals(""))
            return 0;

        int max = 1;  int left = -1;

        Map<Character, Integer> chars = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {

            if (chars.get(s.charAt(i)) != null) {
                left = Math.max(chars.get(s.charAt(i)), left);
            }

            max = Math.max(i - left, max);
            chars.put(s.charAt(i), i);
        }

        return max;
    }
}
